package cn.clq.ssmshiro.service;

import cn.clq.ssmshiro.pojo.User;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Service;

@Service
public class UserService {

    public boolean checkUser(User user){
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(user.getUsername(),user.getPassword());
        try {
            subject.getSession().setAttribute("user",user);
            subject.login(token);
//            subject.checkRole("admin");
            return true;
        }catch (AuthenticationException e){
            return false;
        }
    }

    public boolean logout() {
        Subject subject = SecurityUtils.getSubject();
        try {
            subject.logout();
            return true;
        }catch (AuthenticationException e){
            return false;
        }
    }
}
